﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using NKD07.Classes;

namespace NKD07
{
    public partial class cart : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!IsPostBack)
            //{
                int idproduct = int.Parse(Request.QueryString["productID"].Trim());
                Basket gio = (Basket)Session["GioHang"];
                if (gio.Add(idproduct, int.Parse(quantity.Value)) == true)
                {
                    //gio.Add(idproduct, int.Parse(quantity.Value));
                }
                else
                {
                    if (int.Parse(idproducthidden.Value) == 0)
                    {
                        int quanti = 0;
                        idproducthidden.Value = idproduct.ToString();
                        NKD07.Classes.Basket giohang = (Basket)Session["GioHang"];
                        if (gio != null)
                        {
                            DataTable products = giohang.Data;
                            foreach (DataRow dong in products.Rows)
                            {
                                if (int.Parse(dong["ID"].ToString()) == int.Parse(idproducthidden.Value))
                                    quanti = int.Parse(dong["Quantity"].ToString());

                            }
                        }
                        gio.UpdateQuantity(int.Parse(idproducthidden.Value), quanti);
                    }
                    else
                    {
                        gio.UpdateQuantity(int.Parse(idproducthidden.Value), int.Parse(quantity.Value));
                    }
                }
                    if (int.Parse(idproducthiddendelete.Value) == 0)
                    {

                    }
                    else
                    {
                        gio.Remove(int.Parse(idproducthiddendelete.Value));
                    }
            showcart();
        }
        public void showcart()
        {
                StringBuilder sb = new StringBuilder();
                NKD07.Classes.Basket gio = (Basket)Session["GioHang"];
                long total=0;
                if (gio != null)
                {
                    DataTable products = gio.Data;
                    foreach (DataRow row in products.Rows)
                    {

                        sb.AppendFormat("<tr valign='top' class='sectiontableentry1 png'>");
                        sb.AppendFormat("<td class='first'>");
                        sb.AppendFormat("<span><a href=''><strong>");
                        sb.AppendFormat(row["DisplayName"].ToString());
                        sb.AppendFormat("</strong></a><br>");
                        sb.AppendFormat("</span>");
                        sb.AppendFormat("</td>");
                        sb.AppendFormat("<td class='second'>");
                        sb.AppendFormat("<span>PR28</span>");
                        sb.AppendFormat("</td>");
                        sb.AppendFormat("<td class='three'>");
                        sb.AppendFormat("<span>$ ");
                        sb.AppendFormat(row["Price"].ToString());
                        sb.AppendFormat("</span>");
                        sb.AppendFormat("</td>");
                        sb.AppendFormat("<td class='four'>");
                        sb.AppendFormat("<span><strong class='left1'>");
                        sb.AppendFormat("<input type='text' title='Update Quantity In Cart' id='");
                        sb.AppendFormat("valuequantity" + row["ID"].ToString());
                        sb.AppendFormat("' class='inputbox' size='4' maxlength='4' name='quantity' value='");
                        sb.AppendFormat(row["Quantity"].ToString());
                        sb.AppendFormat("'>");
                        sb.AppendFormat("<input type='image' name='update' title='Update Quantity In Cart' src='./images/update_quantity_cart.png' onclick='getquantity(");
                        sb.AppendFormat(row["ID"].ToString());
                        sb.AppendFormat(")'");
                        sb.AppendFormat("alt='Update' align='middle'>");
                        sb.AppendFormat("</strong><strong class='left2'>");
                        sb.AppendFormat("<input type='image' name='delete' title='Delete Product From Cart' src='./images/remove_from_cart.png' onclick='delete_elementcart(");
                        sb.AppendFormat(row["ID"].ToString());
                        sb.AppendFormat(")'");
                        sb.AppendFormat("alt='Delete Product From Cart' align='middle'>");
                        sb.AppendFormat("</strong></span>");
                        sb.AppendFormat("</td>");
                        sb.AppendFormat("<td class='last'>");
                        sb.AppendFormat("<span>$ ");
                        sb.AppendFormat(Convert.ToString(int.Parse(row["Quantity"].ToString()) * long.Parse(row["Price"].ToString())));
                        sb.AppendFormat("</span>");
                        sb.AppendFormat("</td>");
                        sb.AppendFormat("</tr>");
                        total += int.Parse(row["Quantity"].ToString()) * long.Parse(row["Price"].ToString());
                    }
                    sb.AppendFormat("<tr class='sectiontableentry1 aft'>");
                                     sb.AppendFormat("<td colspan='4' align='right'>");
                                         sb.AppendFormat("<span>Subtotal:</span>");
                                     sb.AppendFormat("</td>");
                                     sb.AppendFormat("<td colspan='3' align='left' class='last'>");
                                         sb.AppendFormat("<span>");
                                         sb.AppendFormat(total.ToString());
                    sb.AppendFormat("</span>");
                                     sb.AppendFormat("</td>");
                                 sb.AppendFormat("</tr>");

                    sb.AppendFormat(" <tr class='sectiontableentry1 aft'>");
                                     sb.AppendFormat("<td colspan='4' align='right' valign='top'>");
                                         sb.AppendFormat("<span>Tax Total:</span>");
                                     sb.AppendFormat("</td>");
                                     sb.AppendFormat("<td colspan='3' align='left' class='last'>");
                                         sb.AppendFormat("<span>");
                                         sb.AppendFormat((total*0.1).ToString());
                    sb.Append("</span>");
                                     sb.AppendFormat("</td>");
                                 sb.AppendFormat("</tr>");
                                 sb.AppendFormat("<tr class='sectiontableentry1 aft1'>");
                                     sb.AppendFormat("<td colspan='4' align='right'>");
                                         sb.AppendFormat("<span>Total:</span>");
                                     sb.AppendFormat("</td>");
                                     sb.AppendFormat("<td colspan='3' align='left'>");
                                         sb.AppendFormat("<strong>");
                                         sb.AppendFormat((total * 0.1 +total).ToString());
                    sb.Append("</strong>");
                                     sb.AppendFormat("</td>");
                                 sb.AppendFormat("</tr>");
                    showgiohang.InnerHtml = sb.ToString();
                }
        }
       
    }
}